var SM3Init = () => {

  /*
  Store the calculated ciphertext here, so we can decrypt the message later.
  */
  let ciphertext;

  /*
  Fetch the contents of the "message" textbox, and encode it
  in a form we can use for the encrypt operation.
  */
  function getMessageEncoding() {
    const messageBox = $("#sm3-message");
    let message = messageBox.value;
    if(!message) {
      messageBox.value = '一去二三里';
      message = messageBox.value;
    }
    return encode(message);
  }

  async function encryptFn(encoded){
    return await window.crypto.subtle.digest(
      {
          name: "SM3",
      },
      new Uint8Array(encoded)
    );
  }
  /*
  Get the encoded message, encrypt it and display a representation
  of the ciphertext in the "Ciphertext" element.
  */
  async function encryptMessage(msg) {
    let encoded = getMessageEncoding();
    ciphertext = await encryptFn(encoded);

    $('.sm3 .ciphertext').style.display = 'block';

    let buffer = Array.from(new Uint8Array(ciphertext)); // convert buffer to byte array
    buffer = buffer.map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase(); // convert bytes to hex string
    
    const ciphertextValue = $(".sm3 .ciphertext-value");
    ciphertextValue.classList.add('fade-in');
    ciphertextValue.addEventListener('animationend', () => {
      ciphertextValue.classList.remove('fade-in');
    });
    ciphertextValue.textContent = `${buffer}`;
  }
  const encryptButton =$(".sm3 .encrypt-button");
  encryptButton.addEventListener("click", () => {
    encryptMessage();
  });
  const encryptTest = getTestFn(encryptFn);
  // const performanceButton = $(".sm3 .performance-button");
  // performanceButton.addEventListener("click", async () => {
  //   performanceButton.textContent = '测试中...'
  //   let encoded = getMessageEncoding();

  //   $(".sm3 .performance-result").textContent = await encryptTest(encoded, 10000, ()=>{
  //     $(".sm3 .performance").style.display = 'inline-block';
  //     performanceButton.textContent = '性能测试'
  //   });
  // });
  window.allPerformance = window.allPerformance || [];
  allPerformance.push({
    name: 'SM3', fn: encryptTest, resultId: '#tr-sm3'
  })
};
